package com.yiren.manager.task;

import java.util.Date;

import org.dom4j.swing.LeafTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yiren.core.exception.MyException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrMealCard;
import com.yiren.entity.YrMealCardRechargeRecord;
import com.yiren.manager.ctrl.messhall.YrMealCardController;
import com.yiren.manager.service.iface.cost.YrMealCardRechargeRecordService;
import com.yiren.manager.service.iface.messhall.YrMealCardService;

public class YrMealCardTask {
	
	@Autowired
	private YrMealCardService mealCardService;
	
	@Autowired
	private YrMealCardRechargeRecordService mealCardRechargeRecordService;
	
	/**
	 * 清零所有员工饭卡的赠送余额
	 * @throws MyException 
	 */
	public void resetMealCardPreBanlance() throws MyException{
		String sqlWhere = "person_type=2 AND pre_balance>0";//2为职工
		QueryResult<YrMealCard> qrMealCard = mealCardService.find(YrMealCard.class, sqlWhere, new Object[]{});
		for(YrMealCard mealCard : qrMealCard.getResultList()){
			if(mealCard.getPreBalance()>0){
				Float preBalance = mealCard.getPreBalance();
				mealCard.setPreBalance(0F);
				mealCardService.save(mealCard);
				//增加一条饭卡充值记录（清零记录）
				YrMealCardRechargeRecord mealCardRechargeRecord = 
						costMealCardRechargeRecord(mealCard, "-1", -preBalance, 0F, 5, 1);
				mealCardRechargeRecordService.save(mealCardRechargeRecord);
			}
		}
	}
	
	/**
	 * 
	 * @param mealCard 饭卡
	 * @param createId 创建者id
	 * @param rechargeMoney 充值金额
	 * @param pracMoney 实充金额
	 * @param payType 充值(1现金2钱包3包餐4赠送充值5赠送余额清零)
	 * @param rechargeType 充值类型(1赠送2自主充值)
	 * @return
	 */
	public YrMealCardRechargeRecord costMealCardRechargeRecord(YrMealCard mealCard,String createId,Float rechargeMoney,Float pracMoney,Integer payType,Integer rechargeType){
		YrMealCardRechargeRecord mealCardRechargeRecord = new YrMealCardRechargeRecord();
		mealCardRechargeRecord.setMealCardRechargeRecordId(UUIDGenerator.getUUID());
		mealCardRechargeRecord.setCardPersonId(mealCard.getCardPersonId());
		mealCardRechargeRecord.setCardCardId(mealCard.getCardCardId());
		mealCardRechargeRecord.setCardPersonName(mealCard.getCardPersonName());
		mealCardRechargeRecord.setCardType(mealCard.getCardType());
		mealCardRechargeRecord.setCreateTime(new Date());
		mealCardRechargeRecord.setCreateId(createId);
		mealCardRechargeRecord.setPersonType(mealCard.getPersonType());
		mealCardRechargeRecord.setRechargeMoney(rechargeMoney);
		mealCardRechargeRecord.setRechargeDate(new Date());
		mealCardRechargeRecord.setRechargeSource(payType);
		mealCardRechargeRecord.setPracRechargeMoney(pracMoney);
		mealCardRechargeRecord.setRechargeType(rechargeType);
		
		return mealCardRechargeRecord;
	}
}
